package com.yoomiito.app.adapter.home.reommend

import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.bean.BannerAndIconInfo
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import kotlinx.android.synthetic.main.item_featured_area.view.*

class FeaturedAreaAdapter(private val context: Context, private val layoutHelper: LayoutHelper, private var datas: List<BannerAndIconInfo>)
    : DelegateAdapter.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {
    var onItemClickListener: (position: Int) -> Unit = {}
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder =
            ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_featured_area, parent, false))

    override fun getItemCount(): Int = if (datas.isNullOrEmpty()) 0 else datas.size

    override fun onCreateLayoutHelper(): LayoutHelper = layoutHelper

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        var itemViewHolder: ItemViewHolder = holder as ItemViewHolder
        if (position%2==0){
            if (position==0){
                itemViewHolder.itemView.ll_p.setPadding(DensityUtil.dp2px(12f),DensityUtil.dp2px(8f),DensityUtil.dp2px(4.5f),DensityUtil.dp2px(9f))
            }else{
                itemViewHolder.itemView.ll_p.setPadding(DensityUtil.dp2px(12f),DensityUtil.dp2px(0f),DensityUtil.dp2px(4.5f),DensityUtil.dp2px(9f))
            }
        }else{
            if (position==1){
                itemViewHolder.itemView.ll_p.setPadding(DensityUtil.dp2px(4.5f),DensityUtil.dp2px(8f),DensityUtil.dp2px(12f),DensityUtil.dp2px(9f))
            }else{
                itemViewHolder.itemView.ll_p.setPadding(DensityUtil.dp2px(4.5f),DensityUtil.dp2px(0f),DensityUtil.dp2px(12f),DensityUtil.dp2px(9f))
            }
        }

        val lp = itemViewHolder.itemView.cardView.layoutParams as LinearLayout.LayoutParams
        lp.width = (App.mWidthPx - DensityUtil.dp2px(33f))/2
        lp.height = (lp.width * 0.55).toInt()
        itemViewHolder.itemView.cardView.layoutParams = lp


        ImageLoader.getInstance().into(context, datas[position].image_url, itemViewHolder.itemView.iv,R.drawable.default_featured_area)
        itemViewHolder.itemView.iv.setOnClickListener { onItemClickListener(position) }

    }

    override fun getItemViewType(position: Int): Int = 11

    private class ItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView)
}